package net.awpspace.mobile.form;

import javax.microedition.midlet.MIDlet;

import net.awpspace.mobile.Application;

import com.sun.lwuit.Command;
import com.sun.lwuit.Form;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;

/**
 * Form abstraction
 * 
 * @author Diep
 * 
 */
public abstract class BaseForm extends Form implements ActionListener {

	protected MIDlet app;
	protected BaseForm parent;

	protected Command cmdBack;

	public BaseForm(MIDlet app, BaseForm parent) {
		super();
		this.app = app;
		this.parent = parent;
		setSmoothScrolling(true);

		cmdBack = new Command("Back");
		setBackCommand(cmdBack);
		addCommand(cmdBack);
		addCommandListener(this);
	}

	public final void actionPerformed(ActionEvent e) {
		if (e.getCommand().equals(cmdBack)) {
			if (parent == null) {
				app.notifyDestroyed();
			} else {
				if (parent instanceof MenuForm) {
					((Application) app).setCategoryBar(null, false);
				}
				parent.showBack();
			}
		} else {
			processCommand(e);
		}
	}

	/**
	 * Process other commands
	 * 
	 * @param e
	 */
	protected abstract void processCommand(ActionEvent e);
}
